package com.lagou.controller;

import com.lagou.service.CodeService;
import com.lagou.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/code")
public class CodeController {


    @Autowired
    private CodeService codeService;

    @Autowired
    private EmailService emailService;

    @RequestMapping("/create/{email}")
    public boolean create(@PathVariable String email){
        //随机生成6位数字
        int code = (int) ((Math.random() * 9 + 1) * 100000);
        //存储code
        codeService.addCode(email,code+"");
        //调用邮箱服务
        boolean result = emailService.sendCode(email,code+"");
        return result;
    }

    @GetMapping("/validate/{email}/{code}")
    public int validate(@PathVariable String email, @PathVariable String code){
        int result = codeService.selectCode(email, code);
        return result;
    }
}
